package pers.zwy.DMSystem.dormitoryManagement.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import pers.zwy.DMSystem.dormitoryManagement.dao.BuildDao;
import pers.zwy.DMSystem.dormitoryManagement.domain.Build;
import pers.zwy.DMSystem.util.dao.impl.BaseDaoImpl;

public class BuildDaoImpl extends BaseDaoImpl implements BuildDao {

	public String getInsertSql() {
		return "insert into t_build (number,floor,room)values(?,?,?)";
	}

	public void setInsertSql(Object data, PreparedStatement pstm) throws Exception {
		Build build = (Build)data;
		pstm.setInt(1, build.getNumber());
		pstm.setInt(2, build.getFloor());
		pstm.setInt(3, build.getRoomOfFloor());
	}

	public String getDeleteSql() {
		return "delete from t_build where id=?";
	}

	public void setDeleteSql(int id, PreparedStatement pstm) throws Exception {
		pstm.setInt(1, id);
	}

	public String getUpdateSql() {
		return "update t_build set number=?,floor=?,room=? where id=?";
	}

	public void setUpdateSql(Object data, PreparedStatement pstm) throws Exception {
		Build build = (Build)data;
		pstm.setInt(1, build.getNumber());
		pstm.setInt(2, build.getFloor());
		pstm.setInt(3, build.getRoomOfFloor());
		pstm.setInt(4, build.getId());

	}

	public String getFindByIdSql() {
		return "select *from t_build where id=?";
	}


	public void setFindByIdSql(int id, PreparedStatement pstm) throws Exception {
		pstm.setInt(1, id);

	}

	public String getFindAllSql() {
		return "select *from t_build";
	}

	public String getFindByConditionSql(Object data) throws Exception {
		StringBuilder sql = new StringBuilder("select *from t_build where 1=1");
		Build build = (Build)data;
		if(build.getId()!=0) {
			sql.append(",id="+build.getId());
		}
		if(build.getNumber()!=0){
			sql.append(",number="+build.getNumber());
		}
		if(build.getFloor()!=0) {
			sql.append(",floor="+build.getFloor());
		}
		if(build.getRoom()!=0) {
			sql.append(",room="+build.getRoomOfFloor());
		}
		return new String(sql);
	}

	public Object rsToObject(ResultSet rs) throws Exception {
		Build build = new Build();
		build.setId(rs.getInt("id"));
		build.setNumber(rs.getInt("number"));
		build.setFloor(rs.getInt("floor"));
		build.setRoom(rs.getInt("room"));
		return build;
	}

}
